package cn.iocoder.yudao.module.biz.service.safetyCheck;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.safetyCheck.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.safetyCheck.BizSafetyCheckDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.safetyCheck.BizSafetyCheckConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.safetyCheck.mapper.BizSafetyCheckMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 安全检查 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizSafetyCheckServiceImpl implements BizSafetyCheckService {

    @Resource
    private BizSafetyCheckMapper safetyCheckMapper;

    @Override
    public String createSafetyCheck(BizSafetyCheckCreateReqVO createReqVO) {
        // 设置单位
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setDeptId(loginUser.getDeptId()+"");
        // 插入
        BizSafetyCheckDO safetyCheck = BizSafetyCheckConvert.INSTANCE.convert(createReqVO);
        safetyCheckMapper.insert(safetyCheck);
        // 返回
        return safetyCheck.getId();
    }

    @Override
    public void updateSafetyCheck(BizSafetyCheckUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSafetyCheckExists(updateReqVO.getId());
        // 更新
        BizSafetyCheckDO updateObj = BizSafetyCheckConvert.INSTANCE.convert(updateReqVO);
        safetyCheckMapper.updateById(updateObj);
    }

    @Override
    public void deleteSafetyCheck(String id) {
        // 校验存在
        this.validateSafetyCheckExists(id);
        // 删除
        safetyCheckMapper.deleteById(id);
    }

    private void validateSafetyCheckExists(String id) {
        if (safetyCheckMapper.selectById(id) == null) {
            throw exception(SAFETY_CHECK_NOT_EXISTS);
        }
    }

    @Override
    public BizSafetyCheckDO getSafetyCheck(String id) {
        return safetyCheckMapper.selectById(id);
    }

    @Override
    public List<BizSafetyCheckDO> getSafetyCheckList(Collection<String> ids) {
        return safetyCheckMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizSafetyCheckDO> getSafetyCheckPage(BizSafetyCheckPageReqVO pageReqVO) {
        return safetyCheckMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizSafetyCheckDO> getSafetyCheckList(BizSafetyCheckExportReqVO exportReqVO) {
        return safetyCheckMapper.selectList(exportReqVO);
    }

    @Override
    public List<BizSafetyCheckPageJoinVO> selectSafetyCheck(BizSafetyCheckPageReqVO pageVO){
        return safetyCheckMapper.selectSafetyCheck(pageVO);
    }

    @Override
    public Integer  selectSafetyCheckCount(BizSafetyCheckPageReqVO pageVO){
        return safetyCheckMapper.selectSafetyCheckCount(pageVO);
    }

}
